/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Table;

import BLL.Customer_BLL;
import DTO.Customer_DTO;
import java.util.HashSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author Nguyen Phi Hung
 */
public class table_CustomerModel extends AbstractTableModel{   
    private int rows, columns;
    private JXTable table;
    
    public Object getCustomerIDbyRow(int rowIndex){
        return getTable().getValueAt(getRows(), 0);
    }
    
    public Customer_DTO getCustomerbyRow(int rowIndex){
        Customer_DTO cus = new Customer_DTO();
        cus.setCustomerID((String) getTable().getValueAt(rowIndex, 0));
        cus.setFullName((String) getTable().getValueAt(rowIndex, 1));
        cus.setHomeAddress((String) getTable().getValueAt(rowIndex, 2));
        cus.setIDNumber((String) getTable().getValueAt(rowIndex, 3));
        cus.setEmail((String) getTable().getValueAt(rowIndex, 4));
        cus.setPhone((String) getTable().getValueAt(rowIndex, 5));
        return cus;
    }
    /**
     * load table khach hang
     * @return
     * @throws Exception 
     */
        public DefaultTableModel loadTable() throws Exception{
        Vector<Customer_DTO> customers;
        Customer_BLL customer_bll = new Customer_BLL();
        Vector vtColumn = new Vector();
        Vector vtData = new Vector();
        customers = customer_bll.GetCustomers("select * from customer", "");
        vtColumn.add("Mã khách hàng");
        vtColumn.add("Họ tên khách hàng");
        vtColumn.add("Địa chỉ");
        vtColumn.add("Số CMND");
        vtColumn.add("Email");
        vtColumn.add("Số điện thoại");
        vtColumn.add("Số sổ đã đăng ký");
        
        for(int i = 0; i < customers.size(); i++){
            Customer_DTO customer = customer_bll.getCustomerByIndex(customers, i);
            Vector vtRow = new Vector();
            
            vtRow.add(customer.getCustomerID());
            vtRow.add(customer.getFullName());
            vtRow.add(customer.getHomeAddress());
            vtRow.add(customer.getIDNumber());
            vtRow.add(customer.getEmail());
            vtRow.add(customer.getPhone());
            vtRow.add(customer.getNumber_SavingBook());

            vtData.add(vtRow);
        }
        return new DefaultTableModel(vtData, vtColumn);
    }

    @Override
    public int getRowCount() {
        return this.rows;
    }

    @Override
    public int getColumnCount() {
        return this.columns;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return table.getValueAt(rows, columns);
    }

    /**
     * @return the rows
     */
    public int getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(int rows) {
        this.rows = rows;
    }

    /**
     * @return the columns
     */
    public int getColumns() {
        return columns;
    }

    /**
     * @param columns the columns to set
     */
    public void setColumns(int columns) {
        this.columns = columns;
    }

    /**
     * @return the table
     */
    public JXTable getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(JXTable table) {
        this.table = table;
    }
}
